#include"shell.h"
#include"user_print.h"
#include "stm32f405xx.h"
#include"swd.h"
static void dap_r(unsigned int argc ,char*argv[])
{
    unsigned int data=0;
    if(argc<2)
    {
        DAP_Read(0,0x0|0x2,&data);
        user_printf("dap 0x00 = 0x%x\r\n",data);
    }
    else
    {
        unsigned int addr = 0u;
        addr = get_hex(argv[1]);
        DAP_Read(0,addr|0x2,&data);
        user_printf("dap 0x%02x = 0x%x\r\n",addr,data);
    }
}
SH_CMD(dap_r,"dap read data");
static void dap_w(unsigned int argc ,char*argv[])
{
    if(argc<3)
    {
        user_printf("input parameter less than three\r\n");
    }
    else
    {
        unsigned int addr = 0u;
        unsigned int data = 0u;
        addr = get_hex(argv[1]);
        data = get_hex(argv[2]);
        DAP_Write(0,(addr&(~0x2)),data);
        user_printf("dap 0x%02x = 0x%x\r\n",addr,data);
    }
}
SH_CMD(dap_w,"dap write data");
static dp_r(unsigned int argc ,char*argv[])
{
    if(argc<2)
    {
        user_printf("input parameter less than three\r\n");
    }
    else
    {
        unsigned int addr = 0u;
        unsigned int data = 0u;
        addr = get_hex(argv[1]);
        DP_Read(addr,&data);
        user_printf("dp 0x%02x = 0x%x\r\n",addr,data);
    }
}
SH_CMD(dp_r,"swd dp read opertion");
static void dp_w(unsigned int argc ,char*argv[])
{
    if(argc<3)
    {
        user_printf("input parameter less than three\r\n");
    }
    else
    {
        unsigned int addr = 0u;
        unsigned int data = 0u;
        addr = get_hex(argv[1]);
        data = get_hex(argv[2]);
        DP_Write(addr,data);
        user_printf("dap 0x%02x = 0x%x\r\n",addr,data);
    }
}
SH_CMD(dp_w,"swd dp write opertion");
static ap_r(unsigned int argc ,char*argv[])
{
    if(argc<2)
    {
        user_printf("input parameter less than three\r\n");
    }
    else
    {
        unsigned int addr = 0u;
        unsigned int data = 0u;
        addr = get_hex(argv[1]);
        AP_Read(addr,&data);
        user_printf("ap 0x%02x = 0x%x\r\n",addr,data);
    }
}
SH_CMD(ap_r,"swd ap read opertion");
static void ap_w(unsigned int argc ,char*argv[])
{
    if(argc<3)
    {
        user_printf("input parameter less than three\r\n");
    }
    else
    {
        unsigned int addr = 0u;
        unsigned int data = 0u;
        addr = get_hex(argv[1]);
        data = get_hex(argv[2]);
        AP_Write(addr,data);
        user_printf("ap 0x%02x = 0x%x\r\n",addr,data);
    }
}
SH_CMD(ap_w,"swd dp write opertion");
static void reg_w(unsigned int argc,char*argv[])
{
    if(argc<3)
    {
        user_printf("input parameter less than three\r\n");
    }
    else
    {
        unsigned int addr = 0u;
        unsigned int data = 0u;
        addr = get_hex(argv[1]);
        data = get_hex(argv[2]);
        AP_Write(0x00,0x23000002);//CSW
        AP_Write(0x04,addr);//TAR
        AP_Write(0x0C,data);//DRW
        
        user_printf("reg write 0x%02x = 0x%x\r\n",addr,data);
    }
}
SH_CMD(reg_w,"register write");
static void reg_r(unsigned int argc,char*argv[])
{
    if(argc<2)
    {
        user_printf("input parameter less than three\r\n");
    }
    else
    {
        unsigned int addr = 0u;
        unsigned int data = 0u;
        addr = get_hex(argv[1]);
        data = get_hex(argv[2]);
        AP_Write(0x00,0x23000002);//CSW
        AP_Write(0x04,addr);//TAR
        AP_Read(0x0C,&data);//DRW ?AP?
        AP_Read(0x0C,&data);//DRW
        user_printf("reg read 0x%02x = 0x%x\r\n",addr,data);
    }
}
SH_CMD(reg_r,"register write");
static void dap_init()
{
    SWD_Init();
    DP_Write(0x00,0x1f);
    DP_Write(0x04,0x50000000);//System power-up request Debug power-up request
    DP_Write(0x00,0x1f);//
    
}
SH_CMD(dap_init,"dap inital");